home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14651 < prev    next >
Encoding:
Text File  |  1996-08-05  |  6.4 KB  |  143 lines

  1. Path: aadt.sdt.com!usenet
  2. From: Larry Baker <leb@sdt.com>
  3. Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.win32
  4. Subject: Re: VC++ 4.0 memory allocation slower than in 2.x!!!
  5. Date: Mon, 01 Apr 1996 13:25:59 -0800
  6. Organization: SABRE Decision Technologies
  7. Message-ID: <316049E7.739D@sdt.com>
  8. References: <alanDozpsy.Kn6@netcom.com> <315C4DA0.30C@Bentley.com>
  9. NNTP-Posting-Host: parmail.sdt.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=iso-8859-1
  12. Content-Transfer-Encoding: 8bit
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14. CC: larryb
  15.  
  16. [this is a little long; wade through the quotes to get the
  17. context, and my text starts]
  18.  
  19. Philip McGraw wrote:
  20. > The VC 4.0 C runtime libraries use HeapAlloc and HeapFree instead of
  21. > the suballocation from VirtualAlloc'ed memory that was used in VC 2.x.
  22.  
  23. To Robert Allen White's original request:
  24. > > Can anyone offer any more rational explanation for this than that
  25. > > Microsoft has horrible quality control? I'm having a hard time
  26. > > believing they could do screw up this badly.
  27.  
  28. To which Michal McKinley also replied:
  29. > If you`re that dependant on the speed of malloc (or new since it uses
  30. > malloc anyway) then perhaps you shouldn`t be using any version of it as
  31. > none of them were built with speed in mind at all...
  32.  
  33. And, which Jim Marshall commented
  34. > [re: 2.0 heap management scheme]
  35. > It fragments memory too much, and hence winds up
  36. > allocating to much (if your app does enough new/mallocs you could
  37. > cause even NT to fail with insf. memory).  
  38.  
  39. To the last two comments: this is a perpetual problem with malloc.  The
  40. version that ships with IBM AIX has facilities for handling fragementation
  41. and pre-allocating specific sized blocks which can *dramatically*
  42. speed up code.
  43.  
  44. C++ programs have a tendency to allocate lots of tiny chunks of memory.
  45. This is exceptionally hard to write *any* good memory management
  46. algorithm for without incurring a lot of garbage-collection-like overhead.
  47.  
  48. The only really efficient way to handle it is to use some kind of
  49. profiling utility (or write your own) to try to come up with a
  50. distribution of common allocation sizes, and then pre-allocate
  51. blocks that can handle them.  Then use a smallest-that-fits strategy
  52. with a quick way to look up the "pool" of pre-allocated memory blocks.
  53. In short, roll-your-own optimized {m,c,re}alloc.
  54.  
  55. You might be surprised at how much this can speed up an otherwise
  56. innocuous section of C or C++ code.  Everyone seems to take malloc
  57. and friends for granted.
  58.  
  59. I checked the Win32 help on VirtualAlloc and Heap{Alloc, Create},
  60. and note the following:
  61.  
  62. - VirtualAlloc operates directly on the virtual memory tables; hence,
  63.   it allocates memory pages at a time, where you specify the address
  64.   range to allocate, and has options for locking them in memory, etc.
  65.   But explicit management of the memory provided (suballocation) must
  66.   be done manually.  To those who can check: is the 2.0 *alloc thread-
  67.   safe?
  68.  
  69. - GlobalAlloc operates on 'the heap.'  It appears to be a legacy
  70.   interface to the old Win16/Win32s interface(s).  There is no mention
  71.   of thread-safeness.
  72.  
  73. - HeapAlloc, by default, performs mutual exclusion on its heap
  74.   object argument. This incurrs an unnecessary performance penalty
  75.   if you're a single-threaded app and can be avoided if you allocate
  76.   per-thread heaps if your a multi-threaded app.  Otherwise, it's
  77.   necessary, either at the OS or at the runtime level.  See the excerpt
  78.   from my Win32 Online Help, below.
  79.  
  80. - VirtualAlloc appears to be the low-level interface to the virtual
  81.   memory subsystem. GlobalAlloc appears to be a general heap manager,
  82.   and does not differentiate between the old local or global heaps.
  83.   HeapAlloc appears to be a complete generalization of heap management,
  84.   accounting for thread-safeness at the OS level.
  85.  
  86. - In the version of the help file which I have (which is probably
  87.   pretty dated), there's not much detail on which one you should choose.
  88.   I speculate that if you're intrested in speed enough to write your own
  89.   versions of malloc() and free(), you'd probably want to do it on top
  90.   of VirtualAlloc, since it appears to be the lowest-level function. But
  91.   bear in mind that you'll have to do your own memory map, as the
  92.   routine expects an address range to map into virtual memory.  And,
  93.   if you're multi-threaded, you'll have to handle multiple heaps (a la
  94.   HeapAlloc) or mutual exclusion (a la HeapAlloc).
  95.  
  96. - I speculate that they changed over to Heap{Alloc, et al} due to
  97.   their explicit thread-fastness at the OS level.  I further speculate
  98.   that Microsoft probably doesn't really care much whether you're
  99.   running a 486SX/33 or a quad-processor 200mhz Pentium Pro.
  100.  
  101. - This sort of thing can't help but bias benchmarks, as evidenced
  102.   by last months' Byte Magazine on the subject.  They discovered a
  103.   similar performance sensitivity to malloc: sometimes (at least, with
  104.   MS 4.x) it returns byte-aligned allocations, rather than word
  105.   aligned.  This causes roughly a 50% (20%?) performance penalty on the
  106.   Pentium for misaligned 32-bit or greater reads, as the hardware has
  107.   to do some monkey business for things like floats, doubles, ints...
  108.  
  109. - It would be useful if someone with 4.x could check their online
  110.   reference materials to see if they can illuminate the subject further.
  111.  
  112. From the Win32 Online Help accompnaying my Borland C++ v4.52:
  113.  
  114. (bein quoted text)
  115.  
  116. [Per the arguments to HeapCreate, HeapAlloc et al:]
  117. [Search for HeapCreate]
  118.  
  119. If the HEAP_NO_SERIALIZE flag is not specified (the simple default),
  120. the heap will serialize access within the calling process. Serialization
  121. ensures mutual exclusion when two or more threads attempt to simultaneously
  122. allocate or free blocks from the same heap. There is a small  performance
  123. cost to serialization, but it must be used whenever multiple threads
  124. allocate and free memory from the same heap.  Setting the
  125. HEAP_NO_SERIALIZE flag eliminates mutual exclusion on the heap. Without
  126. serialization, two or more threads that use the same  heap handle might
  127. attempt to allocate or free memory simultaneously, likely causing corruption
  128. in the heap. The HEAP_NO_SERIALIZE flag can, therefore, be safely used
  129. only in the following situations: 
  130.  
  131. ╖    The process has only one thread. 
  132. ╖    The process has multiple threads, but only one thread calls the
  133.     heap functions for a specific heap. 
  134. ╖    The process has multiple threads, and the application provides
  135.     its own mechanism for mutual exclusion to a specific heap. 
  136.  
  137. (end quoted text)
  138.  
  139. Cheers,
  140.  
  141. Larry Baker
  142. leb@sdt.com
  143.